package com.minisoft.jd.seckill;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import cn.hutool.cron.CronUtil;
import cn.hutool.cron.task.Task;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;
import java.util.TimeZone;
import java.util.concurrent.Executors;

/**
 * @author JueSu
 * @description: JDMX
 * @date 2021/11/26 22:03
 */
@Slf4j
public class Main {

    private static String checkoutCron;

    public static void main(String[] args){
        TimeZone.setDefault(TimeZone.getTimeZone("Asia/Shanghai"));
        checkoutCron = StrUtil.blankToDefault(System.getenv("CHECKOUT_INFO_CRON"),"35 0/20 5,7,9,11,13,15,17,19,21,23 * * *");
        log.info("CHECKOUT_INFO_CRON = {}",checkoutCron);

        if (args.length == 0) {
            args = new String[] {"default"};
        }
        String loginWithEmail = System.getProperty("loginWithEmail","false");
        String retryOrderInterval = System.getProperty("retryOrderInterval");

        List<Killer> killers = new ArrayList<>();
        for (String profile : args) {
            Killer killer = new Killer(profile,loginWithEmail,retryOrderInterval);
//            killer.buyNow();
//            killer.delOrderInCancel();
//            killer.delOrderInRecycle();
//            killer.geteAction();
//            killer.getFromYuyueList();
//            String orderInfo = killer.getOrderInfo();
//            killer.toYuyue("100028389222");
//            killer.exec("100015280612",true);
            killer.start();
            killers.add(killer);
        }

        //设置定期发送订购商品信息
        setupCheckedInfo(killers);

        //设置定期发送下单信息
        setupOrderInfo(killers);

        // 支持秒级别定时任务
        CronUtil.setMatchSecond(true);
//        CronUtil.getScheduler().setThreadExecutor(Executors.newFixedThreadPool(killers.size()));
        CronUtil.start();

    }

    private static void setupOrderInfo(List<Killer> killers) {
        List<String> msgList = new ArrayList<>();
        CronUtil.schedule("0 3 0,6,8,10,12,14,16,18,20,22 * * *", new Task() {
            public void execute() {
                msgList.clear();
                boolean bSend = false;
                msgList.add("<h2>成功抢购的订单：</h2>");
                for (Killer killer : killers) {
                    String orderInfo = killer.getOrderInfo();
                    if (!StrUtil.isBlank(orderInfo)) {
                        bSend = true;
                        msgList.add(orderInfo);
                    }
                }
                msgList.add("<h2>抢购的日志：</h2>");
                for (Killer killer : killers) {
                    String orderInfo = killer.getOrderLog();
                    if (!StrUtil.isBlank(orderInfo)) {
                        bSend = true;
                        msgList.add(orderInfo);
                    }
                }
                if (bSend) {
                    MailSender.sendMsgList("抢购结果通知",msgList);
                }

            }
        });
    }

    private static void setupCheckedInfo(List<Killer> killers) {
        List<String> msgList = new ArrayList<>();
        CronUtil.schedule(checkoutCron, new Task() {
            public void execute() {
                msgList.clear();
                boolean bSend = false;
                for (Killer killer : killers) {
                    String orderInfo = killer.getCheckedInfo();
                    if (!StrUtil.isBlank(orderInfo)) {
                        bSend = true;
                        msgList.add(orderInfo);
                    }
                }
                if (bSend) {
                    MailSender.sendMsgList("抢购列表通知", msgList);
                }
            }
        });
    }

}
